<?php


class IssueService extends Service
{
  /**
   * @param string $className
   * @return IssueService
   */
  public static function instance($className = __CLASS__)
  {
    return parent::instance($className);
  }


  /**
   * 发表问题
   * @param array $data
   * @return array|bool
   */
  public function post($data = array())
  {
    $issue = new Issue();
    $issue->attributes = $data;
    return $issue->save() ? true : $issue->errors;
  }

  /**
   * 获取问题和答案
   * @param int $issue_id
   * @param bool $with
   * @return CActiveRecord
   */
  public function getIssue($issue_id = 0, $with = false)
  {
    if ($with) {
      $data = Issue::model()->with(array('reply', 'reply.user'))->findByPk($issue_id);
    } else {
      $data = Issue::model()->findByPk($issue_id);
    }
    return $data;
  }

  /**
   * 获取问题回复
   * @param int $issue_id
   * @param bool $with
   * @param int $pageSize
   * @return array
   */
  public function getReplyList($issue_id = 0, $with = false, $pageSize = 10)
  {
    $criteria = new CDbCriteria();
    $criteria->condition = 'issue_reply.issue_id=:issue_id';
    $criteria->params = array(
        ':issue_id' => $issue_id
    );
    if ($with) {
      $criteria->with = array(
          'issue',
          'user'
      );
    } else {
      $criteria->with = array(
          'user'
      );
    }
    $count = IssueReply::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);
    $criteria->offset = $pages->currentPage * $pageSize;
    $criteria->limit = $pageSize;
    $criteria->order = 'issue_reply.reply_id desc';
    $list = IssueReply::model()->findAll($criteria);
    return array(
        'list' => $list,
        'pages' => $pages
    );
  }

  /**
   * 获取问题列表
   * @param int $pageSize
   * @param bool $with
   * @param int $user_id
   * @param string $keywords
   * @return array
   */
  public function getList($pageSize = 20, $with = true, $user_id = 0, $keywords = '')
  {
    $criteria = new CDbCriteria();
    //显示某用户的
    if ($user_id > 0) {
      $criteria->condition = 'issue.user_id = :user_id';
      $criteria->params = array(':user_id' => $user_id);
    }
    //关键字
    if (!empty($keywords)) {
      $criteria->addSearchCondition('issue.title', $keywords);
    }
    //关联
    if ($with) {
      $criteria->with = array(
          'reply',
          'user'
      );
    } else {
      $criteria->with = array(
          'user'
      );
    }
    $criteria->order = 'issue.state asc,issue.created_at desc';
    $count = Issue::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);
    $criteria->offset = $pages->currentPage * $pages->pageSize;
    $list = Issue::model()->findAll($criteria);
    return array(
        'list' => $list,
        'pages' => $pages
    );
  }

  /**
   * 回答问题
   * @param array $data
   * @return array|bool
   */
  public function reply($data = array())
  {
    $reply = new IssueReply();
    $reply->attributes = $data;
    return $reply->save() ? true : $reply->errors;
  }

  /**
   * 采纳答案
   * @param int $issue_id
   * @param int $reply_id
   * @return array|bool
   */
  public function accept($issue_id = 0, $reply_id = 0)
  {
    $issue = Issue::model()->findByPk($issue_id);
    //检测问题发起人
    if ($issue->user_id != Yii::app()->user->id) {
      return '对不起，你无权进行该操作!';
    }
    $issue->state = 1;;
    $issue->solve_at = time();
    $issue->reply_id = $reply_id;
    return $issue->save() ? true : $issue->errors;
  }


  /**
   * 获取用户回复
   * @param int $user_id
   * @param bool $with
   * @param int $pageSize
   * @return array
   */
  public function getUserReplyList($user_id = 0, $with = false, $pageSize = 20)
  {
    $criteria = new CDbCriteria();
    $criteria->condition = 'issue_reply.user_id=:user_id';
    $criteria->params = array(
        ':user_id' => $user_id
    );
    if ($with) {
      $criteria->with = array(
          'issue'
      );
    }
    $count = IssueReply::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);
    $criteria->offset = $pages->currentPage * $pageSize;
    $criteria->limit = $pageSize;
    $criteria->order = 'issue_reply.reply_id desc';
    $list = IssueReply::model()->findAll($criteria);
    return array(
        'list' => $list,
        'pages' => $pages
    );
  }

  /**
   * 删除问题
   * @param array $list
   * @return bool
   */
  public function deleteIssue($list = array())
  {
    $criteria = new CDbCriteria();
    $criteria->addInCondition('issue_id', $list);
    return Issue::model()->deleteAll($criteria) ? true : false;
  }

  /**
   * 获取所有问题回复
   * @param int $pageSize
   * @param string $keywords
   * @return array
   */
  public function getAllReplys($pageSize = 20, $keywords = '')
  {
    $criteria = new CDbCriteria();
    $criteria->with = array(
        'issue',
        'user'
    );
    if(!empty($keywords)){
      $criteria->addSearchCondition('issue_reply.content',$keywords);
    }
    $count = IssueReply::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);
    $criteria->offset = $pages->currentPage * $pageSize;
    $criteria->limit = $pageSize;
    $criteria->order = 'issue_reply.reply_id desc';
    $list = IssueReply::model()->findAll($criteria);
    return array(
        'list' => $list,
        'pages' => $pages
    );
  }

  /**
   * 删除回复
   * @param array $list
   * @return bool
   */
  public function deleteReply($list = array())
  {
    $criteria = new CDbCriteria();
    $criteria->addInCondition('reply_id', $list);
    return IssueReply::model()->deleteAll($criteria) ? true : false;
  }
} 